package com.company.cloud.platform.modules.mgr.controller;

import com.alibaba.fastjson.JSON;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.auth.annotation.StaffLogc;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.company.modules.api.request.*;
import com.company.cloud.company.modules.api.response.CompanyFull;
import com.company.cloud.company.modules.api.response.CompanyFullPage;
import com.company.cloud.company.modules.api.service.CompanyRemoteService;
import com.company.cloud.platform.modules.mgr.controller.req.CompanyQuery4PageParam;
import com.company.cloud.platform.modules.mgr.controller.req.CompanyUpdateStateParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 平台公司信息管理
 */
@Authc
@RestController
@RequestMapping("/admin/mgr/company")
@Api(tags = "Admin_MGR_公司信息管理")
public class CompanyMgrController {

    @Autowired
    private CompanyRemoteService companyRemoteService;

    @PostMapping("/query")
    @ApiOperation(value = "查询", response = CompanyFull.class)
    public BaseResult query(@RequestBody CompanyQuery4PageParam params) {
        CompanyQueryRequest request = new CompanyQueryRequest();
        request.setCompanyNo(params.getCompanyNo());
        request.setName(params.getName());
        request.setType(params.getType());
        request.setSourceType(params.getSourceType());
        request.setSource(params.getSource());
        request.setPage(params.getPage());
        request.setRows(params.getRows());
        BaseClientResult<CompanyFullPage> dataResult = companyRemoteService.queryCompanyFull(request);
        System.out.println(JSON.toJSONString(dataResult));
        CompanyFullPage data = BaseClientResult.getResultData2Error(dataResult);

        return BaseResult.ok(data);
    }


    @PostMapping("/add")
    @ApiOperation(value = "新增", response = CompanyFull.class)
    @StaffLogc(title = "新增公司信息", action = "新增公司信息", actionAdo = "公司信息管理")
    public BaseResult add(@RequestBody Company2UserAddRequest params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        params.setOpIp(userInfo.getIp());
        params.setOpUserId(userInfo.getId());
        BaseClientResult<CompanyFull> data = companyRemoteService.addCompany2User(params);
        CheckUtils.checkClientResult(data);
        return BaseResult.ok(data.getData());
    }


    @PostMapping("/edit")
    @ApiOperation(value = "修改", response = CompanyFull.class)
    @StaffLogc(title = "修改公司信息", action = "修改公司信息", actionAdo = "公司信息管理")
    public BaseResult edit(@RequestBody CompanyUpdateRequest params) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        params.setOpIp(userInfo.getIp());
        params.setOpUserId(userInfo.getId());
        BaseClientResult<CompanyFull> data = companyRemoteService.updateCompany(params);
        CheckUtils.checkClientResult(data);
        return BaseResult.ok(data.getData());
    }

    @GetMapping("/get")
    @ApiOperation(value = "获取", response = CompanyFull.class)
    public BaseResult get(@RequestParam("companyNo") String companyNo) {
        BaseClientResult<CompanyFull> data = companyRemoteService.getCompanyFull(companyNo);
        CheckUtils.checkClientResult(data);
        return BaseResult.ok(data.getData());
    }


    @PostMapping("/check")
    @ApiOperation(value = "审核", response = BaseResult.class)
    @StaffLogc(title = "审核公司信息", action = "审核公司信息", actionAdo = "公司信息管理")
    public BaseResult check(@RequestBody CompanyCheckRequest params) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        params.setOpIp(userInfo.getIp());
        params.setOpUserId(userInfo.getId());
        BaseClientResult<CompanyFull> data = companyRemoteService.checkCompany(params);
        CheckUtils.checkClientResult(data);
        return BaseResult.ok();
    }

    @PostMapping("/frozen")
    @ApiOperation(value = "冻结", response = BaseResult.class)
    @StaffLogc(title = "冻结公司信息", action = "冻结公司信息", actionAdo = "公司信息管理")
    public BaseResult frozen(@RequestBody CompanyUpdateStateParam params) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        CompanyFrozenRequest request = new CompanyFrozenRequest();
        request.setCompanyNo(params.getCompanyNo());
        request.setOpIp(userInfo.getIp());
        request.setOpUserId(userInfo.getId());
        BaseClientResult<CompanyFull> data = companyRemoteService.frozenCompany(request);
        CheckUtils.checkClientResult(data);
        return BaseResult.ok();
    }


    @PostMapping("/unfreeze")
    @ApiOperation(value = "解冻", response = BaseResult.class)
    @StaffLogc(title = "解冻公司信息", action = "解冻公司信息", actionAdo = "公司信息管理")
    public BaseResult unfreeze(@RequestBody CompanyUpdateStateParam params) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        CompanyUnFreezeRequest request = new CompanyUnFreezeRequest();
        request.setCompanyNo(params.getCompanyNo());
        request.setOpIp(userInfo.getIp());
        request.setOpUserId(userInfo.getId());
        BaseClientResult<CompanyFull> data = companyRemoteService.unfreezeCompany(request);
        CheckUtils.checkClientResult(data);
        return BaseResult.ok();
    }

    @GetMapping("/del")
    @ApiOperation(value = "删除", response = BaseResult.class)
    @StaffLogc(title = "删除公司信息", action = "删除公司信息", actionAdo = "公司信息管理")
    public BaseResult del(@RequestParam("companyNo") String companyNo) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        CompanyDelRequest request = new CompanyDelRequest();
        request.setCompanyNo(companyNo);
        request.setOpIp(userInfo.getIp());
        request.setOpUserId(userInfo.getId());
        BaseClientResult<CompanyFull> data = companyRemoteService.delCompany(request);
        CheckUtils.checkClientResult(data);
        return BaseResult.ok();
    }


}
